//token变量
var token = "";
var code;
//datagrid数据
function po(a) {
    //创建datagrid
    $('#dg').datagrid({
        data: a,
        scrollbarSize: 8,
        singleSelect: true,
        border: false,
        striped: true,
        columns: [[
            { field: 'carrier', title: '承运商', width: 100, align: 'center', },
            { field: 'channelName', title: '渠道名称', width: 120, align: 'center' },
            { field: 'summaryConversion', title: '折算总费用', width: 100, align: 'center' },
            { field: 'freightOilBearingUnitPrice', title: '含油单价', width: 100, align: 'center' },
            {
                field: 'freightFuelCostsRate', title: '燃油费率', width: 100, align: 'center', formatter: function (value) {
                    if (value == null) {
                        return "<span>无</span>"
                    } else {
                        return "<span>" + value + "%</span>"
                    }
                }
            },
            { field: 'freightUnitPrice', title: '单价', width: 100, align: 'center' },
            { field: 'freightFormula', title: '运费公式', width: 130, align: 'center' },
            {
                field: 'fees', title: '运费', width: 100, align: 'center', formatter: function (value, row, index) { return value[0].value }
            },
            { field: 'freightFuelCostsValue', title: '运费燃油费', width: 100, align: 'center' },
            { field: 'freightCurrency', title: '币种', width: 100, align: 'center' },
            {
                field: 'discount', title: '折扣', width: 100, align: 'center', formatter: function (value) {
                    if (value == null) {
                        return "<span>无</span>"
                    } else {
                        return "<span>" + value + "</span>"
                    }
                }
            },
            { field: 'totalSurchargeStr', title: '加收附加费', width: 100, align: 'center' },
            { field: 'exchange', title: '汇率', width: 100, align: 'center' },
            { field: 'priceNo', title: '编号', width: 100, align: 'center' },
            { field: 'forbiddenGoods', title: '禁忌', width: 150, align: 'center' },
            { field: 'timeCost', title: '时效', width: 100, align: 'center' },
            {
                field: 'tags', title: '标签', width: 100, align: 'center', formatter: function (value) {
                    if (value == "") {
                        return "<span>无</span>"
                    } else {
                        return "<span>" + value + "</span>"
                    }

                }
            }
        ]]
    });
}

token = sessionStorage.getItem('token');
var headers = {
    'Content-Type': "application/json",
    'Authorization': token
};

//运费查询
function price(sub_data) {
    $.ajax({
        url: "http://lex.itdida.com/itdida-api/price",
        type: 'POST',
        headers: headers,
        data: JSON.stringify(sub_data),
        dataType: 'json',
        success: function (data) {
            if (data.statusCode == 200) {
                if (data.data.length == 0) {
                    alert("没有查询到数据!");
                    po(data.data);
                }
                if (data.data.length > 0) {
                    po(data.data);
                }
            }
        },
        error: function (data) {
            alert(jQuery.parseJSON(data.responseText).data);

        }
    })
}

//运费查询点击事件
function yunfei() {
    $("#com_m").addClass("active");
    $("#com_m2").addClass("active");
    $("#dgg").addClass("active");
    $(".yunfei").addClass("active");
    $(".active_c").addClass("active");
    $("#btn").addClass("active");
    var country = code;
    var packageType = $(".dian").val();
    var weight = $(".zhong").val();
    var pieceCount = $("#pieceCount").val();
    var subdata = {
        "city": "string",
        "priceZoneType": 0,    //分区类型(0:国家 1:邮编 2:港口 3:城市)
        "searchType": 2,
        "importExport": 0,     //0出口 1进口
        "countryCode": country,   //国家1                
        "packageType": packageType,      //0文件 1包裹(默认) 2包裹袋1
        "pieceCount": pieceCount,       //件数 1            
        "weight": weight           //重量        1
    }
    price(subdata);
    $("#dg").datagrid("resize");
}

//订单查询
function dingdan() {
    var no = $("#two").val();
    $.ajax({
        url: "http://lex.itdida.com/itdida-api/queryYunDanDetail",
        type: 'GET',
        headers: headers,
        data: { 'danHaos': no },
        dataType: 'json',
        success: function (data) {
            $("#accordion").html("");
            for (var i = 0; i < data.data.length; i++) {
                var city = data.data[i].city;
                var country = data.data[i].guoJia;
                var kehuDH = data.data[i].keHuDanHao;
                var shouhuo = data.data[i].shouHuoQuDao;
                var xitongDH = data.data[i].xiTongDanHao;
                var zhuanDH = data.data[i].zhuanDanHao;
                var nums = data.data[i].numOfUnits;
                if (zhuanDH == "") {
                    zhuanDH = "无";
                }
                if (city == "") {
                    city = "无";
                }
                if (data.data[i].errorMsg == "") {
                    $("#accordion").append('<div class="panel panel-default"><div class="panel-heading Logistics_info clearfix" data-toggle="collapse" data-parent="#accordion" href="#collapse' + i + '">' +
                        '<div class="Logistics_info1"><div class="center-between"><p><span>系统单号:</span>' + xitongDH + '</p><p><span>客户单号:</span>' + kehuDH + '</p> <p><span>转单号:</span>' + zhuanDH + '</p><p><span>件数:</span>' + nums + '</p></div> <div class="center-between flow"> <div class="sender"> <p><span>国家:</span>' + country + '</p></div>    <div class="status"> <p><span>城市:</span>' + city + '</p> </div> <div class="receiver"><p><span>收获渠道:</span>' + shouhuo + '</p> </div></div></div>'
                        + '</div><div id="collapse' + i + '" class="panel-collapse collapse"><div class="panel-body"><div class="delivery-item"><div class="routes-wrapper" ><div class="route-list" id="xinxi' + i + '"> </div></div></div></div></div></div>')
                }
                else {
                    $("#accordion").append('<div class="panel panel-default"><div class="panel-heading Logistics_info clearfix" data-toggle="collapse" data-parent="#accordion" href="#collapse' + i + '">  运单号不存在 </div></div>')
                }
            }
            //运单轨迹查询
            $.ajax({
                url: "http://lex.itdida.com/itdida-api/queryTracks",
                type: 'GET',
                headers: headers,
                data: { 'no': no },
                dataType: 'json',
                success: function (data) {
                    console.log(data.data);
                    for (var i = 0; i < data.data.length; i++) {
                        for (var b = 0; b < data.data[i].trackList.length; b++) {
                            var desc = data.data[i].trackList[b].desc;
                            var time = data.data[i].trackList[b].time;
                            var localtion = data.data[i].trackList[b].localtion;
                            if (data.data[i].trackList.length > 0) {
                                $("#xinxi" + i).append(' <div class="solid-line"></div><ul class="route first">' +
                                    ' <li class="route-status-text">' +
                                    ' <span class="signed">' + desc + '</span>' +
                                    '   </li>' +
                                    '    <li class="route-status-icon">' +
                                    '   </li>' +
                                    '  <li class="route-date-time">' +
                                    '  <span>' + time + '</span>' +
                                    '   </li>' +
                                    '   <li class="route-desc">' +
                                    '    <span>' + localtion + '</span>' +
                                    '  </li>' +
                                    '    </ul>  ');
                            }
                        }
                        if (data.data[i].trackList.length == 0) {
                            $("#xinxi" + i).append('<span>暂无物流消息！</span>');
                        }
                    }
                }
                ,
                error: function (data) {
                    alert(jQuery.parseJSON(data.responseText).data);
                }
            })
        },
        error: function (data) {
            alert(jQuery.parseJSON(data.responseText).data);
        }
    })
}

$(function () {
    //运费查询
    $("#btn>button").click(function () {
        //运费查询
        if (token == '' || token == 'undefined' || token == undefined || token == NaN || token == 'NaN') {
            alert("请登陆后查询！");
            window.location.href = "login.html";
        }
        else {
            if ($("#country").val() == "") {
                alert("请输入国家!");
                return false;
            }
            if ($("#pieceCount").val() == "") {
                alert("请输入件数!");
                return false;
            }
            if ($(".zhong").val() == "") {
                alert("请输入重量!");
                return false;
            }
            yunfei();
        }
    })
    //运单查询
    $("#sch").click(function () {
        dingdan();
    })
    //焦点事件
    sh();
})

//显示遮罩层
function showMask() {
    $("#mask").css("height", $('window').height());
    $("#mask").css("width", $('window').width());
    $("#mask").show();
}

//关闭遮罩层
function EndMask() {
    $("#mask").hide();
}

//焦点事件
function sh() {

}
$(function () {
    $(".pagination li").click(function () {
        $(this).addClass("active").siblings().removeClass('active');
    })
    $(".btn1").click(function () {
        layer.tab({
            area: ['755px', '662px'],
            tab: [{
                title: '运单详情',
                content: $('#Waybill_details1').html()
                // content: ''
            }, {
                title: '电子存根',
                content: $('#Electronic_invoice').html()
            }]
        });
    })
})

var active_li = GetQueryString('active_li');
var active_span = GetQueryString('active_span');
var vm = new Vue({
    el: '#app',
    data: {
        active_li: active_li ? active_li : 1,
        active_span: active_span ? active_span : 11,
        block: 'block',
        none: 'none',
    },
    methods: {
        handleLi: function (li, span) {
            this.active_li = li;
            this.active_span = span;
        },
        handleClick: function (li, span) {
            this.active_li = li;
            this.active_span = span;
        }
    }
})
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]); return null;
}

//国家列表
$("#QueryFreight_CountryName").on('click', function () {
    $("#DivCountryCode").show();
    //form的下拉式页签选单
    $('#DivCountryCode span').on('click', function (e) {
        $("#QueryFreight_CountryName").val($(this).text());
        $("#DivCountryCode").hide();;
        code = $(this).attr("data-value");
    });
});

/*=======快速寄件模块=======*/